package com.guoli.print;

import java.io.IOException;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

public class TestPrintC {
	public static void main(String[] args) throws PrintException, IOException, InterruptedException {
		PrintService service = PrintServiceLookup.lookupDefaultPrintService();
		DocPrintJob job = service.createPrintJob();
		String ii = "^PCT~~CD,~CC^~CT~"+
					"^XA~TA000~JSN^LT0^MNM^MTT^PON^PMN^LH0,0^JMA^PR2,2~SD24^JUS^LRN^CI0^XZ"+
					"^XA"+
					"^MMT"+
					"^PW639"+
					"^LL0320"+
					"^LS0"+
					"^FO9,42^GB563,233,1^FS"+
					"^FO9,75^GB561,0,1^FS"+
					"^FO9,133^GB562,0,1^FS"+
					"^FO9,166^GB562,0,1^FS"+
					"^FO9,196^GB561,0,1^FS"+
					"^CW1,E:simsun.fnt"+
					"^Fo63,48^A1,18,18,^CI17^F8^FD上 海 国 利 车 真 皮 饰 件 有 限 公 司^FS"+
					"^FO28,80^A1,18,18,^CI17^F8^FD零 件 名 称 ：^FS"+
					"^FO180,80^A1,18,18,^CI17^F8^FD前 座 垫^FS"+
					"^FO300,80^A1,18,18,^CI17^F8^FD驾 驶^FS"+
					"^FO180,104^A1,14,13,^CI17^F8^FDSGM615^FS"+
					"^FO28,136^A1,14,15,^CI17^F8^FD零  件  号  ：^FS"+
					"^FO180,136^A1,17,15,^CI17^F8^FD2206794^FS"+
					"^FO28,170^A1,17,15,^CI17^F8^FD生 产 日 期 ：^FS"+
					"^FO180,170^A1,17,15,^CI17^F8^FD2013年 05 月 08 日^FS"+
					"^FO479,170^A1,17,15,^CI17^F8^FD班^FS"+
					"^BY3,3,30^FT24,266^BCN,,Y,Y"+
					"^FD>911113110101634101911102048^FS"+
					"^FT215,185^AAN,27,15^FB231,1,0,C^FH\\^FD++?**0B*3+*4P^FS"+
					"^PQ1,0,1,Y^XZ";
		byte[] url = ii.getBytes();
		Doc doc = new SimpleDoc(url, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
		PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
		attrs.add(new Copies(1));
		job.print(doc, attrs);
//		String filePath = "";
//		String shellCmd = "/usr/bin/lp -d " + service.getName() + " -o raw " + filePath;
//		Process pr = Runtime.getRuntime().exec(new String[]{"bash", "-c", shellCmd});
//		pr.waitFor();
		
		job.addPrintJobListener(new PrintJobAdapter() {

			@Override
			public void printDataTransferCompleted(PrintJobEvent pje) {
				System.out.println("printDataTransferCompleted");
			}

			@Override
			public void printJobCompleted(PrintJobEvent pje) {
				System.out.println("printJobCompleted");
			}

			@Override
			public void printJobFailed(PrintJobEvent pje) {
				System.out.println("printJobCompleted");
			}

			@Override
			public void printJobCanceled(PrintJobEvent pje) {
				System.out.println("printJobCanceled");
			}

			@Override
			public void printJobNoMoreEvents(PrintJobEvent pje) {
				System.out.println("printJobNoMoreEvents");
			}

			@Override
			public void printJobRequiresAttention(PrintJobEvent pje) {
				System.out.println("printJobNoMoreEvents");
			}
			
		});
	}
}
